﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Principal;

namespace RestNews.Util.Security
{
    /// <summary>
    /// Logged User Identity
    /// </summary>
    public class UserIdentity : IIdentity
    {
        #region Public Members

        /// <summary>
        /// Gets the user id.
        /// </summary>
        public long PersonId { get; private set; }

        /// <value>
        /// The first name.
        /// </value>
        public string FirstName { get; private set; }

        /// <value>
        /// The last name.
        /// </value>
        public string LastName { get; private set; }

        /// <value>
        /// The username.
        /// </value>
        public string Username { get; private set; }

        /// <value>
        /// The address.
        /// </value>
        public string Address { get; private set; }

        /// <summary>
        /// Gets or sets the roles.
        /// </summary>
        public List<string> Roles { get; private set; }

        #endregion

        /// <summary>
        /// Gets the current user identity.
        /// </summary>
        /// <value>
        /// The current user identity.
        /// </value>
        public static UserIdentity CurrentUserIdentity { get { return HttpContext.Current.User.Identity as UserIdentity; } }

        /// <summary>
        /// Initializes a new instance of the <see cref="UserIdentity"/> class.
        /// </summary>
        /// <param name="firstName">The first name.</param>
        /// <param name="lastName">The last name.</param>
        /// <param name="address">The address.</param>
        /// <param name="username">The username.</param>
        /// <param name="roles">The roles.</param>
        public UserIdentity(long personId, string firstName, string lastName, string address, string username, IEnumerable<string> roles)
        {
            PersonId = personId;
            FirstName = firstName;
            LastName = lastName;
            Address = address;
            Username = username;
            Roles = new List<string>();
            Roles.AddRange(roles);
        }

        #region IIdentity Implementation
        
        /// <summary>
        /// Gets the type of authentication used.
        /// </summary>
        /// <returns>The type of authentication used to identify the user.</returns>
        public string AuthenticationType
        {
            get { return "Digest"; }
        }

        /// <summary>
        /// Gets a value that indicates whether the user has been authenticated.
        /// </summary>
        /// <returns>true if the user was authenticated; otherwise, false.</returns>
        public bool IsAuthenticated
        {
            get { return !string.IsNullOrEmpty(Username); }
        }

        /// <summary>
        /// Gets the name of the current user.
        /// </summary>
        /// <returns>The name of the user on whose behalf the code is running.</returns>
        public string Name
        {
            get { return string.Format("{0} {1}", FirstName, LastName); }
        }

        #endregion
    }
}